<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ page contentType="text/html; charset=utf-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>平台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layuiadmin/layui/css/layui.css"
          media="all">
    <link rel="stylesheet" href="/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="/css/plat-style.css" media="all">
    <style>
        thead tr th {
            text-align: center !important;
        }

        tr td {
            text-align: center !important;
        }

    </style>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">
				<span class="layui-breadcrumb" lay-filter="breadcrumb">
	              <a href="javascript:;">角色管理</a>
	            </span>
                </div>
                <div class="layui-card-body layui-form">
                    <%-- 查询条件设置 --%>
                    <div class="test-table-reload-btn layui-col-sm-offset10" style="margin-bottom: 10px;">
                        <button class="layui-btn addThis" style="margin-left: 10px"
                        >新增角色
                        </button>
                    </div>
                    <%-- 数据展示 --%>
                    <table class="layui-table" id="table" lay-filter="table"></table>
                    <script type="text/html" id="toolbar">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-warm layui-btn-xs" lay-event="delThis">删除</button>
                            {{# if(d.status == 1){ }}
                            <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateThis">禁用</button>
                            {{# } }}
                            {{# if(d.status == 0){ }}
                            <button class="layui-btn layui-btn-normal layui-btn-xs" lay-event="updateThis">启用</button>
                            {{# } }}
                        </div>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="/layuiadmin/layui/layui.js"></script>
<script src="/js/common.js"></script>
<script>
    layui.config({
        base: '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate', 'table'], function () {
        var $ = layui.$, layer = layui.layer, form = layui.form,
            element = layui.element, laydate = layui.laydate, table = layui.table;
        form.render('select');
        element.render();
        laydate.render({
            elem: '#laydate-cn',
            range: '~'
        });

        table.render({
            elem: '.layui-table',
            url: '/base/selectRolesList',
            page: true,
            request: {
                pageName: 'current', //页码的参数名称，默认：page
                limitName: 'size' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'id', title: 'ID'},
                {field: 'name', title: '角色名称'},
                {
                    field: 'status', title: '状态',
                    templet: function (d) {
                        if (d.status == 1) {
                            return '正常'
                        } else if (d.status == 0) {
                            return '禁用'
                        }
                    }
                },
                {
                    field: 'createtime', title: '创建时间',
                    templet: "<div>{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm')}}</div>"
                },
                {title: '操作', templet: '#toolbar'}
            ]],
            skin: "row",
            even: true,
            size: "lg"
        });


        var active = {
            editThis: function (id) {
                layer.open({
                    title: '新增/编辑角色',
                    type: 2,
                    area: ['850px', '500px'],
                    content: '/base/roleEdit?id=' + id
                });
            },
            delThis: function (id) {
                layer.confirm('确定删除吗？', function () {
                    $.ajax({
                        url: "/base/deleteRole",
                        data: {'id': id},
                        dataType: "json",
                        type: "post",
                        success: function (status) {
                            if (status) {
                                layer.msg('删除成功', {icon: 1});
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1000)
                            } else {
                                layer.msg('删除失败', {icon: 2})
                            }
                        }
                    })
                });
            },
            updateThis: function (id, status) {
                layer.confirm('确定操作吗？', function () {
                    $.ajax({
                        url: "/base/handleRoleStatus",
                        data: {'id': id, "status": status},
                        dataType: "json",
                        type: "post",
                        success: function (data) {
                            if (data.status) {
                                layer.msg(data.desc, {icon: 1});
                                setTimeout(function () {
                                    window.location.reload();
                                }, 1000)
                            } else {
                                layer.msg(data.desc, {icon: 2});
                            }
                        }
                    })
                });
            }
        };
        $('.addThis').on('click', function () {
            var type = 'editThis';
            active[type] && active[type].call(this, "");
        });

        //导出事件触发
        $(".exportThis").on('click', function () {
            var exportThis = $(this).data('type');
            var bid = $('select[name="bid"]').find('option:selected').val();
            var orderType = $('select[name="orderType"]').find('option:selected').val();
            var payType = $('select[name="payType"]').find('option:selected').val();
            var orderStatus = $('select[name="orderStatus"]').find('option:selected').val();
            var phone = $('input[name="phone"]').val();
            var orderCode = $('input[name="orderCode"]').val();
            var payCode = $('input[name="payCode"]').val();
            var queryTime = $('input[name="queryTime"]').val();
            active[exportThis] && active[exportThis].call(this, bid, orderType, payType, orderStatus, phone, orderCode, payCode, queryTime);
        });

        //监听事件
        table.on('tool(table)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            if (layEvent === 'addThis') {
                active[layEvent] && active[layEvent].call(this);
            } else if (layEvent === 'editThis') {
                active[layEvent] && active[layEvent].call(this, data.id);
            } else if (layEvent === 'delThis') {
                active[layEvent] && active[layEvent].call(this, data.id);
            } else if (layEvent === 'updateThis') {
                var status = 0;
                if (data.status == 0) {
                    status = 1;
                } else if (data.status == 1) {
                    status = 0;
                }
                active[layEvent] && active[layEvent].call(this, data.id, status);
            }
        });

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('table', {
                where: field
            });
        });
    })
</script>
</body>
</html>